package man

import (
	"dub/define/grpcmodel"
	"dub/secrec"
	"regexp"
)

type AdvertManController struct {
	AdvertBaseController
}

func (a *AdvertManController) Get() {
	a.TplName = "man/advertMan.html"
}

func (a *AdvertManController) ToAdd() {
	a.TplName = "man/toAddAdvert.html"
}

func (a *AdvertManController) Add() {
	a.TplName = "man/toAddAdvert.html"
	msg := "参数错误"
	for {
		adTitle := a.GetString("adTitle", "")
		if adTitle == "" {
			break
		}
		adUrl := a.GetString("adUrl", "")
		adUrl_vali, _ := regexp.MatchString(`[a-zA-z]+://[^\s]*`, adUrl)
		if !adUrl_vali {
			break
		}
		adCSPrice, _ := a.GetFloat("adCSPrice", 0.00)
		if adCSPrice <= 0 {
			break
		}
		adTCount, _ := a.GetUint32("adTCount", 0)
		if adTCount <= 0 {
			break
		}

		args := &grpcmodel.ModelSecRpcAdvertReqModel{
			IsAllUpdate: false,
			ModelSecRpcAdvert: &grpcmodel.ModelSecRpcAdvert{
				AdTitle:   adTitle,
				AdUrl:     adUrl,
				AdCSPrice: adCSPrice,
				AdTCount:  adTCount,
				AdType:    1,
				AdCount:   0,
				AdStatus:  1,
			},
		}
		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("man_advert_controller.go Add method not find use_rpc info GetRpc\n")
			break
		}
		sec_advert := grpcmodel.NewISecRpcAdvertClient(use_rpc.Conn())
		reply, err := sec_advert.Add(use_rpc.Context(), args)
		if err != nil {
			a.Log().Errorf("man_advert_controller.go add method sec_advert.Add err. %v\n", err)
			break
		}

		if reply.GetErr() > 0 {
			msg = "添加失败"
		}

		a.TplName = "man/advertMan.html"
		break
	}
	a.Data["msg"] = msg
}

func (a *AdvertManController) FindAdverts() {
	page, _ := a.GetUint32("page", 1)
	pageSize, _ := a.GetUint32("rows", 20)
	rows := make([]*grpcmodel.ModelSecRpcAdvert, 0)
	jsons := make(map[string]interface{})
	jsons["total"] = 0
	jsons["rows"] = rows

	for {
		args := &grpcmodel.ModelSecRpcAdvertReqFind{
			StartRow: (page - 1) * pageSize,
			PageSize: pageSize,
		}
		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("man_advert_controller.go FindAdverts method no find use_rpc info GetRpc\n")
			break
		}
		sec_advert := grpcmodel.NewISecRpcAdvertClient(use_rpc.Conn())
		reply, err := sec_advert.Find(use_rpc.Context(), args)
		if err != nil {
			a.Log().Errorf("man_advert_controller.go FindAdverts method sec_advert.Find err. %v\n", err)
			break
		}
		if reply.GetErr() > 0 {
			break
		}
		rows = reply.GetRows()
		jsons["rows"] = rows
		jsons["total"] = reply.GetTotal()
		break
	}

	a.Data["json"] = jsons
	a.ServeJSON()
}

func (a *AdvertManController) ToUp() {
	a.TplName = "man/toUpdateAdvert.html"
	for {
		id, _ := a.GetUint32("id", 0)
		if id < 1 {
			a.TplName = "man/advertMan.html"
			break
		}

		args := &grpcmodel.ModelSecRpcAdvertReqFind{
			AdvertId: id,
		}
		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("man_advert_controller.go ToUp method no find use_rpc info GetRpc\n")
			break
		}
		sec_advert := grpcmodel.NewISecRpcAdvertClient(use_rpc.Conn())
		reply, err := sec_advert.FindAdvert(use_rpc.Context(), args)
		a.Log().Infof("update advert reply info is %+v", *reply)
		if err != nil {
			a.Log().Errorf("man_advert_controller.go ToUp method sec_advert.FindAdvert err. %v\n", err)
			break
		}
		if reply.GetErr() > 0 {
			break
		}

		a.Data["advert"] = *(reply.GetModelSecRpcAdvert())
		break
	}
}

func (a *AdvertManController) Up() {
	a.TplName = "man/toUpdateAdvert.html"
	msg := "参数错误"
	for {
		id, _ := a.GetUint32("id", 0)
		adTitle := a.GetString("adTitle", "")
		if adTitle == "" {
			break
		}
		adUrl := a.GetString("adUrl", "")
		adUrl_vali, _ := regexp.MatchString(`[a-zA-z]+://[^\s]*`, adUrl)
		if !adUrl_vali {
			break
		}
		adCSPrice, _ := a.GetFloat("adCSPrice", 0.00)
		if adCSPrice <= 0 {
			break
		}
		adTCount, _ := a.GetUint32("adTCount", 0)
		if adTCount <= 0 {
			break
		}

		args := &grpcmodel.ModelSecRpcAdvertReqModel{
			IsAllUpdate: false,
			ModelSecRpcAdvert: &grpcmodel.ModelSecRpcAdvert{
				AdvertId:  id,
				AdTitle:   adTitle,
				AdUrl:     adUrl,
				AdCSPrice: adCSPrice,
				AdTCount:  adTCount,
			},
		}
		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("man_advert_controller.go Up method not find use_rpc info GetRpc\n")
			break
		}
		sec_advert := grpcmodel.NewISecRpcAdvertClient(use_rpc.Conn())
		reply, err := sec_advert.Update(use_rpc.Context(), args)
		if err != nil {
			a.Log().Errorf("man_advert_controller.go Up method sec_advert.Add err. %v\n", err)
			break
		}

		if reply.GetErr() > 0 {
			msg = "修改失败"
		}

		a.TplName = "man/advertMan.html"
		break
	}
	a.Data["msg"] = msg

}

func (a *AdvertManController) Action() {
	jsons := make(map[string]interface{})
	jsons["result"] = false
	jsons["msg"] = "参数错误"

	for {
		id, _ := a.GetUint32("id", 0)
		if id < 1 {
			break
		}

		model := &grpcmodel.ModelSecRpcAdvert{
			AdvertId: id,
		}

		action_type, _ := a.GetUint8("type", 0)
		ispast := true
		switch action_type {
		case 1: //启用
			model.AdStatus = 2
		case 2: //禁用
			model.AdStatus = 1
		case 3: //添加浏览量
			adcount, _ := a.GetUint32("count", 0)
			if adcount < 1 {
				ispast = false
				break
			}
			model.AdCount = adcount
		default:
			ispast = false
		}
		if !ispast {
			break
		}

		args := &grpcmodel.ModelSecRpcAdvertReqModel{
			IsAllUpdate:       false,
			ModelSecRpcAdvert: model,
		}
		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("man_advert_controller.go Action method not find use_rpc info GetRpc\n")
			break
		}
		sec_advert := grpcmodel.NewISecRpcAdvertClient(use_rpc.Conn())
		reply, err := sec_advert.Update(use_rpc.Context(), args)
		if err != nil {
			a.Log().Errorf("man_advert_controller.go Action method sec_advert.Add err. %v\n", err)
			break
		}

		if reply.GetErr() > 0 {
			jsons["msg"] = "修改失败"
		} else {
			jsons["result"] = true
		}
		break
	}

	a.Data["json"] = jsons
	a.ServeJSON()
}
